/* 
 * Copyright 2013 Sébastien Mouquet.
 * 
 * This file is part of ECovoiturage.
 * 
 * ECovoiturage is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ECovoiturage is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Foobar. If not, see http://www.gnu.org/licenses/.
 */
package com.smouquet.ecovoiturage.core.service.impl;

import static com.google.gwt.thirdparty.guava.common.base.Preconditions.checkArgument;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.smouquet.ecovoiturage.core.bean.Config;
import com.smouquet.ecovoiturage.core.dbmaintenance.ScriptRepository;
import com.smouquet.ecovoiturage.core.dbmaintenance.ScriptRunner;
import com.smouquet.ecovoiturage.core.exception.RunScriptException;
import com.smouquet.ecovoiturage.core.service.DbMaintenanceService;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;

/**
 *
 * @author sebastien.mouquet
 */
public class DbMaintenanceServiceImpl implements DbMaintenanceService
{
    @Inject @Named("derby")
    private ScriptRepository derbyScriptRepository;
    
    @Inject @Named("postgres")
    private ScriptRepository postgresScriptRepository;
    

    @Override
    public ScriptRepository getScriptRepository(Config config)
    {
        checkArgument(config != null, "config is required");
        checkArgument(config.getJdbcDriver() != null && !config.getJdbcDriver().isEmpty(), "JDBC Driver is required in the configuration");
        
        
        if(config.getJdbcDriver().contains("derby")) {
            return derbyScriptRepository;
        } else if(config.getJdbcDriver().contains("postgres")) {
            return postgresScriptRepository;
        }
        
        return null;
    }

    @Override
    public void runAllScripts(Connection connection, Config config) throws RunScriptException
    {
        try
        {
            ScriptRepository scriptRepository = getScriptRepository(config);
            ScriptRunner scriptRunner = new ScriptRunner(connection, false, true);

            InputStream is;
            InputStreamReader isr;
            for(String script : scriptRepository.getAllScripts())
            {
                is = scriptRepository.getScriptAsStream(script);
                isr = new InputStreamReader(is);

                scriptRunner.runScript(isr);
            }
            
        } catch(IOException ex) {
            throw new RunScriptException("Error during runnig script", ex);
        } catch(SQLException ex) {
            throw new RunScriptException("Error during runnig script", ex);
        }
    }
    
    
}
